package com.springboot.idcard.util;

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;

import java.net.URL;

import static org.opencv.highgui.HighGui.imshow;
import static org.opencv.highgui.HighGui.waitKey;
import static org.opencv.imgcodecs.Imgcodecs.imwrite;
import static org.opencv.imgproc.Imgproc.COLOR_RGB2GRAY;
import static org.opencv.imgproc.Imgproc.cvtColor;

public class OpenCVUtil {
    private static URL url;

    static {
        // 加载动态库
        url = ClassLoader.getSystemResource("lib/opencv/opencv_java342.dll");
        System.load(url.getPath());
    }


    /**
     * 测试方法
     *
     * @param args
     */
    public static void main(String[] args) {
        try {
            Mat image = Imgcodecs.imread("D:\\hello.jpg");
            if (image.empty()) {
                throw new Exception("image is empty");
            }
            imshow("Original Image", image);

        // 创建输出单通道图像
        Mat grayImage = new Mat(image.rows(), image.cols(), CvType.CV_8SC1);
        // 进行图像色彩空间转换
        cvtColor(image, grayImage, COLOR_RGB2GRAY);

        imshow("Processed Image", grayImage);
        imwrite("D://copy_json.jpg", grayImage);
            waitKey();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
